package com.news.web.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.Review;
import com.news.service.ReviewService;
import com.news.service.impl.ReviewServiceImpl;

/**
 * Servlet implementation class AdminReviewDeleteAllServlet
 */
@WebServlet("/AdminReviewDeleteAllServlet")
public class AdminReviewDeleteAllServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String[] reviewIdStr = request.getParameterValues("reviewIdAll");
		// 创建ReviewService实例
		ReviewService reviewService = new ReviewServiceImpl();
		if (reviewIdStr != null) {
			int[] reviewIds = new int[reviewIdStr.length];
			for (int i = 0; i < reviewIdStr.length; i++) {
				// 执行异常抛出，加入传入的str不能转为数字
				try {
					reviewIds[i] = Integer.parseInt(reviewIdStr[i]);
				} catch (NumberFormatException e) {
					// 如果失败，设置为null
					request.setAttribute("reviewDeleteAllBoolean", null);
					// 跳转回原页面
					request.getRequestDispatcher("/admin/adminReview.jsp").forward(request, response);
					return;
				}
			}
			// 执行删除方法
			Boolean reviewDeleteAllBoolean = reviewService.deleteReview(reviewIds);
			// 将数据存入
			request.setAttribute("reviewDeleteAllBoolean", reviewDeleteAllBoolean);
		}
		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminReview.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
